package com.atfusu.interceptor;

import com.atfusu.result.Result;
import com.atfusu.result.ResultCodeEnum;
import com.atfusu.util.WebUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/12/3
 * Description:
 **/
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //验证是否处于登录状态
        Object userInfo = request.getSession().getAttribute("userInfo");
        if (userInfo == null) {
            Result result = Result.build(null, ResultCodeEnum.LOGIN_AUTH);
            WebUtil.writeJSON(response, result);
            return false;
        }
        return true;
    }
}
